package com.wangsd.web.controller;

import com.wangsd.web.utils.HttpUtils;
import com.wangsd.web.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/url")
@Api(tags = "获取随机图片的URL")
public class URLController {

    @ApiModelProperty("图片URL链接")
    private String imageUrl = "https://www.dmoe.cc/random.php?return=json";


    @GetMapping("/image")
    @ApiOperation("发起HTTP请求,获取图片的URL")
    public R getPhotoUrl() {
        String result = HttpUtils.doGet(imageUrl);
        if (null == result) return R.error(0, "无效的图片获取链接");
        return R.success(result);
    }


    @GetMapping("/set")
    @ApiOperation("手动设置图片的URL")
    public R setURL(@RequestParam(name = "url", defaultValue = "") String url) {
        if (imageUrl.equalsIgnoreCase(url)) {
            return R.error(0, "重复的链接");
        }
        this.imageUrl = url;
        // return prefix
        return R.success();
    }
}
